package com.android.boncen.commonFrame.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.bean.GlucoseHistoryBean;
import com.android.boncen.commonFrame.tools.DesityUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 功能描述:
 *
 * @date 2017/2/9
 */

public class AddGlucoseDialog extends AlertDialog {

    private EditText et_popup;
    private RadioGroup radioGroup;

    public AddGlucoseDialog(Context context, SaveCallback callback) {
        super(context, R.style.CustomDialogStyle);
        this.callback = callback;
    }

    public interface SaveCallback {
        void save(int type, float size);
    }

    SaveCallback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow_style);
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        et_popup = (EditText) findViewById(R.id.popup_et_add_blood);
        radioGroup = (RadioGroup) findViewById(R.id.popup_radiogroup);
        findViewById(R.id.popup_bt_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        et_popup.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    String text = et_popup.getText().toString();
                    if (Integer.parseInt(text) > 35) {
                        Toast.makeText(getContext(), "血糖值不能大于 35", Toast.LENGTH_SHORT).show();
                        et_popup.setText("");
                    }
                } catch (NumberFormatException e) {
                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
    }

    private void save() {
        if (TextUtils.isEmpty(et_popup.getText())) {
            Toast.makeText(getContext(), "血糖值不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else {

            int type = 0;
            String bloodNum = et_popup.getText().toString();
            switch (radioGroup.getCheckedRadioButtonId()) {
                case R.id.popupbt_before_bf:
                    type = 0;
                    break;
                case R.id.popupbt_after_bf:
                    type = 1;
                    break;
                case R.id.popupbt_before_lunch:
                    type = 2;
                    break;
                case R.id.popupbt_after_lunch:
                    type = 3;
                    break;
                case R.id.popupbt_before_dinner:
                    type = 4;
                    break;
                case R.id.popupbt_after_dinner:
                    type = 5;
                    break;
                case R.id.popupbt_before_sleep:
                    type = 6;
                    break;
                default:
                    break;
            }
            dismiss();
            callback.save(type, Float.valueOf(bloodNum));
        }
    }
}
